package com.zzb.service.impl;


import com.github.pagehelper.PageHelper;
import com.zzb.DTO.ResultDTO;
import com.zzb.DTO.UserDTO;
import com.zzb.VO.UserVO;
import com.zzb.dao.UserDao;
import com.zzb.entity.UserEntity;
import com.zzb.service.UserService;
import com.zzb.util.Md5;
import com.zzb.util.Tool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    private UserEntity userEntity = new UserEntity();

    @Override
    public ResultDTO login(UserVO userVO, String sessionId) {
//        判断验证码是否正确
        Jedis jedis = new Jedis();
        String vCode = jedis.get(sessionId);
        if (userVO.getCode().equals(vCode)) {

            UserEntity userEntity = Tool.copy(userVO, UserEntity.class);
            String mingwen = userEntity.getPassword();
            String anwen = Md5.encoderByMd5(mingwen);
            userEntity.setPassword(anwen);

            UserEntity model = userDao.findByUsernameAndPassword(userEntity);
            if (model != null) {
                //登录有效时间
                jedis.setex(userEntity.getUsername(),60*60*12,userEntity.getUsername());
                return ResultDTO.getResult("success", 1);
            } else {
                return ResultDTO.getResult("error", 0);
            }

        } else {
            return ResultDTO.getResult("error-code", -1);
        }
    }

    @Override
    public ResultDTO changePasswordById(UserVO userVO) {
        BeanUtils.copyProperties(userVO, userEntity);
        //输入的旧密码
        String oldPassword = userEntity.getOldPassword();
        //输入的新密码
        String password = userEntity.getPassword();
        //根据id查找用户
        userEntity = userDao.findById(userEntity);
        //判断输入的密码与原密码是否相等
        if (userEntity.getPassword().equals(Md5.encoderByMd5(oldPassword))) {
            //将新密码设置进去
            userEntity.setPassword(Md5.encoderByMd5(password));
            return ResultDTO.getResult("success", userDao.update(userEntity));
        } else {
            return ResultDTO.getResult("error", 0);
        }
    }


    @Override
    public ResultDTO add(UserVO userVO) {
        //数据复制
        BeanUtils.copyProperties(userVO, userEntity);
        //判断用户是否存在
        if (userDao.findByName(userEntity) == null) {//不存在
            //加密
            String mingwen = userEntity.getPassword();
            String anwen = Md5.encoderByMd5(mingwen);
            userEntity.setPassword(anwen);
            return ResultDTO.getResult("success", userDao.add(userEntity));
        } else {//用户已存在
            return ResultDTO.getResult("error", 0);
        }

    }

    @Override
    public ResultDTO delete(UserVO userVO) {
        //数据复制
        BeanUtils.copyProperties(userVO, userEntity);
        //判断用户是否存在
        if (userDao.findById(userEntity) != null) {
            //存在
            return ResultDTO.getResult("success", userDao.delete(userEntity));
        } else {//用户不存在
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO findById(UserVO userVO) {
        //复制
        BeanUtils.copyProperties(userVO, userEntity);
        //根据id查找
        userEntity = userDao.findById(userEntity);
        if (userEntity != null) {
            //查找到数据
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(userEntity, userDTO);
            return ResultDTO.getResult("success", userDTO);
        } else {
            //数据没找到
            return ResultDTO.getResult("error", 0);
        }

    }

    @Override
    public ResultDTO findAll(UserVO userVO) {
        //复制数据
        BeanUtils.copyProperties(userVO, userEntity);
        //查询条数
        int count = userDao.count(userEntity);
        //pageHelper分页插件的使用
        PageHelper.startPage(userEntity.getPage(), userEntity.getLimit());
        //数据库查询数据
        List<UserEntity> list = userDao.findAll(userEntity);
        //集合复制
        List<UserDTO> userDTOList = Tool.copy(list, UserDTO.class);
        return ResultDTO.getResult(count, userDTOList);
    }

    @Override
    public ResultDTO update(UserVO userVO) {
        //数据复制
        BeanUtils.copyProperties(userVO, userEntity);
        //根据id查找
        UserEntity userEntityFindById = userDao.findById(userEntity);
        String newName = userEntity.getUsername();
        String oldName = userEntityFindById.getUsername();
        //判断是不是没有修改名字
        if (oldName.equals(newName)) {
            userEntity.setPassword(userEntityFindById.getPassword());
            return ResultDTO.getResult("success", userDao.update(userEntity));
        }
        //根据名称查找用户
        UserEntity userEntityFindByName = userDao.findByName(userEntity);
        if (userEntityFindByName == null) {
            //名称不存在
            userEntity.setPassword(userEntityFindById.getPassword());
            return ResultDTO.getResult("success", userDao.update(userEntity));
        }
        //已存在
        return ResultDTO.getResult("error", 0);
    }
}
